﻿using System;
using System.Reflection;

using NUnit.Framework;

using log4net;
using log4net.Core;

namespace FWF.Logging.Test
{
    /// <summary>
    /// 
    /// </summary>
    [TestFixture]
    public class ExtendedLogImplementationTest
    {

        /// <summary>
        /// Prepares the log event.
        /// </summary>
        [Test]
        public void PrepareLogEvent()
        {
            ILogger logger = LoggerManager.GetLogger( Assembly.GetExecutingAssembly(), typeof ( ExtendedLogImplementationTest ) );
            var extendedLogImplementation = new ExtendedLogImplementation( logger );

            //
            GlobalContext.Properties["instanceName"] = "instanceName";
            LogicalThreadContext.Properties["sessionName"] = "sessionName";
            LogicalThreadContext.Properties["sessionContext"] = "sessionContext";
            LogicalThreadContext.Properties["sessionId"] = 123;

            //
            LoggingEventData loggingEventData = extendedLogImplementation.PrepareLogEvent( typeof( ExtendedLogImplementationTest ) );

            //
            loggingEventData.Level = Level.Debug;
            loggingEventData.Message = "This is a test message";

            LogEventItem logEventItem = LogEventItem.FromLoggingEventData( loggingEventData );

            Assert.IsNotEmpty( logEventItem.Message );
            Assert.IsNotEmpty( logEventItem.Identity );
            Assert.IsNotEmpty( logEventItem.LevelName );
            Assert.IsNotEmpty( logEventItem.LoggerName );
            Assert.IsNotEmpty( logEventItem.MethodName );
            Assert.IsNotEmpty( logEventItem.ThreadName );
            Assert.IsNotEmpty( logEventItem.UserName );

            Assert.Greater( logEventItem.TimeStamp.Ticks, DateTime.MinValue.Ticks );
            Assert.Greater( logEventItem.LevelValue, -1 );

            Assert.IsNotNull( logEventItem.Properties );

            Assert.IsNotEmpty( logEventItem.MachineName );
            Assert.IsNotEmpty( logEventItem.InstanceName );
            Assert.IsNotEmpty( logEventItem.SessionContext );
            Assert.IsNotEmpty( logEventItem.SessionName );
            Assert.Greater( logEventItem.SessionId, -1 );

            // Known issues where these properties are not preserved
            //Assert.Greater( logEventItem.Id, -1 );
            //Assert.IsNotEmpty( logEventItem.ExceptionType );
            
        }
    }
}
